<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Tutorial: Automate backups with SLM | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'getting-started-snapshot-lifecycle-management.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/getting-started-snapshot-lifecycle-management.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/getting-started-snapshot-lifecycle-management.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/getting-started-snapshot-lifecycle-management.html" rel="nofollow" target="_blank">../en/getting-started-snapshot-lifecycle-management.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="snapshot-restore.html">Snapshot and restore</a></span>
»
<span class="breadcrumb-link"><a href="snapshot-lifecycle-management.html">SLM: Manage the snapshot lifecycle</a></span>
»
<span class="breadcrumb-node">Tutorial: Automate backups with SLM</span>
</div>
<div class="navheader">
<span class="prev">
<a href="snapshot-lifecycle-management.html">« SLM: Manage the snapshot lifecycle</a>
</span>
<span class="next">
<a href="slm-and-security.html">Security and SLM »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="getting-started-snapshot-lifecycle-management"></a>Tutorial: Automate backups with SLM<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/slm/getting-started-slm.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>This tutorial demonstrates how to automate daily backups of Elasticsearch indices using an SLM policy.
The policy takes <a class="xref" href="modules-snapshots.html" title="Snapshot module">snapshots</a> of all indices in the cluster
and stores them in a local repository.
It also defines a retention policy and automatically deletes snapshots
when they are no longer needed.</p>
<p>To manage snapshots with SLM, you:</p>
<div class="olist orderedlist">
<ol class="orderedlist">
<li class="listitem">
<a class="xref" href="getting-started-snapshot-lifecycle-management.html#slm-gs-register-repository" title="Register a repository">Register a repository</a>.
</li>
<li class="listitem">
<a class="xref" href="getting-started-snapshot-lifecycle-management.html#slm-gs-create-policy" title="Set up a snapshot policy">Create an SLM policy</a>.
</li>
</ol>
</div>
<p>To test the policy, you can manually trigger it to take an initial snapshot.</p>
<h4>
<a id="slm-gs-register-repository"></a>Register a repository<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/slm/getting-started-slm.asciidoc">edit</a>
</h4>
<p>To use SLM, you must have a snapshot repository configured.
The repository can be local (shared filesystem) or remote (cloud storage).
Remote repositories can reside on S3, HDFS, Azure, Google Cloud Storage,
or any other platform supported by a <a href="https://www.elastic.co/guide/en/elasticsearch/plugins/7.7/repository.html" class="ulink" target="_top">repository plugin</a>.
Remote repositories are generally used for production deployments.</p>
<p>For this tutorial, you can register a local repository from
<a href="https://www.elastic.co/guide/en/kibana/7.7/snapshot-repositories.html" class="ulink" target="_top">Kibana Management</a>
or use the put repository API:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /_snapshot/my_repository
{
  "type": "fs",
  "settings": {
    "location": "my_backup_location"
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1211.console"></div>
<h4>
<a id="slm-gs-create-policy"></a>Set up a snapshot policy<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/slm/getting-started-slm.asciidoc">edit</a>
</h4>
<p>Once you have a repository in place,
you can define an SLM policy to take snapshots automatically.
The policy defines when to take snapshots, which indices should be included,
and what to name the snapshots.
A policy can also specify a <a class="xref" href="slm-retention.html" title="Snapshot retention">retention policy</a> and
automatically delete snapshots when they are no longer needed.</p>
<div class="tip admon">
<div class="icon"></div>
<div class="admon_content">
<p>Don’t be afraid to configure a policy that takes frequent snapshots.
Snapshots are incremental and make efficient use of storage.</p>
</div>
</div>
<p>You can define and manage policies through Kibana Management or with the put policy API.</p>
<p>For example, you could define a <code class="literal">nightly-snapshots</code> policy
to back up all of your indices daily at 1:30AM UTC.</p>
<p>A put policy request defines the policy configuration in JSON:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /_slm/policy/nightly-snapshots
{
  "schedule": "0 30 1 * * ?", <a id="CO461-1"></a><i class="conum" data-value="1"></i>
  "name": "&lt;nightly-snap-{now/d}&gt;", <a id="CO461-2"></a><i class="conum" data-value="2"></i>
  "repository": "my_repository", <a id="CO461-3"></a><i class="conum" data-value="3"></i>
  "config": { <a id="CO461-4"></a><i class="conum" data-value="4"></i>
    "indices": ["*"] <a id="CO461-5"></a><i class="conum" data-value="5"></i>
  },
  "retention": { <a id="CO461-6"></a><i class="conum" data-value="6"></i>
    "expire_after": "30d", <a id="CO461-7"></a><i class="conum" data-value="7"></i>
    "min_count": 5, <a id="CO461-8"></a><i class="conum" data-value="8"></i>
    "max_count": 50 <a id="CO461-9"></a><i class="conum" data-value="9"></i>
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1212.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO461-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>When the snapshot should be taken in
<a class="xref" href="trigger-schedule.html#schedule-cron" title="Cron schedule">Cron syntax</a>: daily at 1:30AM UTC</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO461-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>How to name the snapshot: use
<a class="xref" href="date-math-index-names.html" title="Date math support in index names">date math</a> to include the current date in the snapshot name</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO461-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>Where to store the snapshot</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO461-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>The configuration to be used for the snapshot requests (see below)</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO461-5"><i class="conum" data-value="5"></i></a></p>
</td>
<td align="left" valign="top">
<p>Which indices to include in the snapshot: all indices</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO461-6"><i class="conum" data-value="6"></i></a></p>
</td>
<td align="left" valign="top">
<p>Optional retention policy: keep snapshots for 30 days,
retaining at least 5 and no more than 50 snapshots regardless of age</p>
</td>
</tr>
</table>
</div>
<p>You can specify additional snapshot configuration options to customize how snapshots are taken.
For example, you could configure the policy to fail the snapshot
if one of the specified indices is missing.
For more information about snapshot options, see <a class="xref" href="snapshots-take-snapshot.html" title="Take a snapshot of one or more indices">snapshot requests</a>.</p>
<h4>
<a id="slm-gs-test-policy"></a>Test the snapshot policy<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/slm/getting-started-slm.asciidoc">edit</a>
</h4>
<p>A snapshot taken by SLM is just like any other snapshot.
You can view information about snapshots in Kibana Management or
get info with the <a class="xref" href="snapshots-monitor-snapshot-restore.html" title="Monitor snapshot and restore progress">snapshot APIs</a>.
In addition, SLM keeps track of policy successes and failures so you
have insight into how the policy is working. If the policy has executed at
least once, the <a class="xref" href="slm-api-get-policy.html" title="Get snapshot lifecycle policy API">get policy</a> API returns additional metadata
that shows if the snapshot succeeded.</p>
<p>You can manually execute a snapshot policy to take a snapshot immediately.
This is useful for taking snapshots before making a configuration change,
upgrading, or to test a new policy.
Manually executing a policy does not affect its configured schedule.</p>
<p>Instead of waiting for the policy to run, tell SLM to take a snapshot
using the configuration right now instead of waiting for 1:30 a.m..</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /_slm/policy/nightly-snapshots/_execute</pre>
</div>
<div class="console_widget" data-snippet="snippets/1213.console"></div>
<p>After forcing the <code class="literal">nightly-snapshots</code> policy to run,
you can retrieve the policy to get success or failure information.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_slm/policy/nightly-snapshots?human</pre>
</div>
<div class="console_widget" data-snippet="snippets/1214.console"></div>
<p>Only the most recent success and failure are returned,
but all policy executions are recorded in the <code class="literal">.slm-history*</code> indices.
The response also shows when the policy is scheduled to execute next.</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>The response shows if the policy succeeded in <em>initiating</em> a snapshot.
However, that does not guarantee that the snapshot completed successfully.
It is possible for the initiated snapshot to fail if, for example, the connection to a remote
repository is lost while copying files.</p>
</div>
</div>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "nightly-snapshots" : {
    "version": 1,
    "modified_date": "2019-04-23T01:30:00.000Z",
    "modified_date_millis": 1556048137314,
    "policy" : {
      "schedule": "0 30 1 * * ?",
      "name": "&lt;nightly-snap-{now/d}&gt;",
      "repository": "my_repository",
      "config": {
        "indices": ["*"],
      },
      "retention": {
        "expire_after": "30d",
        "min_count": 5,
        "max_count": 50
      }
    },
    "last_success": {                                                    <a id="CO462-1"></a><i class="conum" data-value="1"></i>
      "snapshot_name": "nightly-snap-2019.04.24-tmtnyjtrsxkhbrrdcgg18a", <a id="CO462-2"></a><i class="conum" data-value="2"></i>
      "time_string": "2019-04-24T16:43:49.316Z",
      "time": 1556124229316
    } ,
    "last_failure": {                                                    <a id="CO462-3"></a><i class="conum" data-value="3"></i>
      "snapshot_name": "nightly-snap-2019.04.02-lohisb5ith2n8hxacaq3mw",
      "time_string": "2019-04-02T01:30:00.000Z",
      "time": 1556042030000,
      "details": "{\"type\":\"index_not_found_exception\",\"reason\":\"no such index [important]\",\"resource.type\":\"index_or_alias\",\"resource.id\":\"important\",\"index_uuid\":\"_na_\",\"index\":\"important\",\"stack_trace\":\"[important] IndexNotFoundException[no such index [important]]\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.indexNotFoundException(IndexNameExpressionResolver.java:762)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.innerResolve(IndexNameExpressionResolver.java:714)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.resolve(IndexNameExpressionResolver.java:670)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndices(IndexNameExpressionResolver.java:163)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndexNames(IndexNameExpressionResolver.java:142)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndexNames(IndexNameExpressionResolver.java:102)\\n\\tat org.elasticsearch.snapshots.SnapshotsService$1.execute(SnapshotsService.java:280)\\n\\tat org.elasticsearch.cluster.ClusterStateUpdateTask.execute(ClusterStateUpdateTask.java:47)\\n\\tat org.elasticsearch.cluster.service.MasterService.executeTasks(MasterService.java:687)\\n\\tat org.elasticsearch.cluster.service.MasterService.calculateTaskOutputs(MasterService.java:310)\\n\\tat org.elasticsearch.cluster.service.MasterService.runTasks(MasterService.java:210)\\n\\tat org.elasticsearch.cluster.service.MasterService$Batcher.run(MasterService.java:142)\\n\\tat org.elasticsearch.cluster.service.TaskBatcher.runIfNotProcessed(TaskBatcher.java:150)\\n\\tat org.elasticsearch.cluster.service.TaskBatcher$BatchedTask.run(TaskBatcher.java:188)\\n\\tat org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:688)\\n\\tat org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:252)\\n\\tat org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:215)\\n\\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\\n\\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\\n\\tat java.base/java.lang.Thread.run(Thread.java:834)\\n\"}"
    } ,
    "next_execution": "2019-04-24T01:30:00.000Z",                        <a id="CO462-4"></a><i class="conum" data-value="4"></i>
    "next_execution_millis": 1556048160000
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO462-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Information about the last time the policy successfully initated a snapshot</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO462-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>The name of the snapshot that was successfully initiated</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO462-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>Information about the last time the policy failed to initiate a snapshot</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO462-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>The next time the policy will execute</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="snapshot-lifecycle-management.html">« SLM: Manage the snapshot lifecycle</a>
</span>
<span class="next">
<a href="slm-and-security.html">Security and SLM »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>